<?php
namespace App\Library\SmsGateway;

use App\Library\Helper;
use App\Library\Http;

class Daredo2 {

    private $gateway = 'http://lc.sms10690.com/LczsInfer/sms/putMt.do?msgFormat=2&corpID=5001023&loginName=MHT2223&password=MHT2223&Mobs=%s&msg=%s&mtLevel=1&subNumber=2223&linkID=&kindFlag=&MD5str=';

    public function mt($mobile, $content) {
        $url    = sprintf($this->gateway, $mobile, urlencode($content));
        $http   = new Http();
        $retMsg = $http->request($url);
        if($retMsg == '') {
            $retMsg = $http->request($url);//重试一次
            if($retMsg == '') {
                return [
                    'status'      => Adapter::ERROR,
                    'status_msg'  => Adapter::ERROR_MESSAGE,
                    'gateway_msg' => '网关没有响应',
                    'send_at'     => Helper::getNow()
                ];
            }
        }
        $retMsg = str_replace(["\r\n", "\n", "\r"], [',', ',', ''], $retMsg);
        $ret    = explode(',', $retMsg);
        if(isset($ret[0])) {
            if($ret[0] == 100) {
                return [
                    'status'      => Adapter::SUCCESS,
                    'status_msg'  => Adapter::SUCCESS_MESSAGE,
                    'gateway_msg' => $retMsg,
                    'send_at'     => Helper::getNow()
                ];
            } else {
                return [
                    'status'      => Adapter::ERROR_GATEWAY,
                    'status_msg'  => Adapter::ERROR_GATEWAY_MESSAGE,
                    'gateway_msg' => $retMsg,
                    'send_at'     => Helper::getNow()
                ];
            }
        } else {
            return [
                'status'      => Adapter::ERROR_GATEWAY,
                'status_msg'  => Adapter::ERROR_GATEWAY_MESSAGE,
                'gateway_msg' => $retMsg,
                'send_at'     => Helper::getNow()
            ];
        }
    }
}
